package com.movitech.mobile.cz.base.purchase.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.purchase.bizvo.PsPurchasePlanListParamVo;
import com.movitech.mobile.cz.base.purchase.entity.PSpecialPurchasePlan;
import com.movitech.mobile.cz.base.purchase.entity.ext.ExtPSpecialPurchasePlan;
import com.movitech.mobile.cz.base.purchase.service.PSpecialPurchasePlanService;
import com.movitech.mobile.cz.base.sys.entity.SCompany;
import com.movitech.mobile.cz.base.sys.entity.SProject;
import com.movitech.mobile.cz.base.sys.service.SCompanyService;
import com.movitech.mobile.cz.base.sys.service.SProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * <p>
 * PSpecialPurchasePlan 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-08
 */
@RestController
@RequestMapping("/v1/pSpecialPurchasePlan")
@Api(value = "专项采购计划基础操作接口", description = "专项采购计划基础操作接口")
public class PSpecialPurchasePlanController {
    @Autowired
    private PSpecialPurchasePlanService pSpecialPurchasePlanService;
    @Autowired
    private SProjectService sProjectService;
    @Autowired
    private SCompanyService sCompanyService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有专项采购计划接口",
            notes = "获取所有专项采购计划接口",
            response = PSpecialPurchasePlan.class
    )
    public RestReturnResult list() {
        RestReturnResult restResult = RestReturnResult.success();
        Page<PSpecialPurchasePlan> page = pSpecialPurchasePlanService.selectPage(new Page<PSpecialPurchasePlan>());
        restResult.setData(page);
        return restResult;
    }

    @PostMapping("/searchList")
    @ApiOperation(
            value = "专项采购计划列表查询接口",
            notes = "专项采购计划列表查询接口",
            response = PSpecialPurchasePlan.class
    )
    public RestReturnResult searchList(@ApiParam(value = "专项采购计划列表入参")@RequestBody PsPurchasePlanListParamVo vo) {
        RestReturnResult restResult = RestReturnResult.success();
        Page<PSpecialPurchasePlan> page = new Page<>();
        page.setCurrent(vo.getPageRequestVO().getPageNumber());
        page.setSize(vo.getPageRequestVO().getPageSize());
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(vo.getPecialPurchasePlan());
        page = pSpecialPurchasePlanService.selectPage(page,wrapper);
        restResult.setData(page);
        return restResult;
    }


    @GetMapping("/getPSpecialPurchasePlan/{id}")
    @ApiOperation(
            value = "专项采购计划对象ID获取接口",
            notes = "专项采购计划对对象ID获取接口",
            response = PSpecialPurchasePlan.class
    )
    public RestReturnResult addPSpecialPurchasePlan(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        PSpecialPurchasePlan pSpecialPurchasePlan = pSpecialPurchasePlanService.selectById(id);
        restResult.setData(pSpecialPurchasePlan);
        return restResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "专项采购计划添加接口",
            notes = "专项采购计划添加接口",
            response = PSpecialPurchasePlan.class
    )
    public RestReturnResult add(@RequestBody PSpecialPurchasePlan pSpecialPurchasePlan) {
        RestReturnResult restResult = RestReturnResult.success();
        pSpecialPurchasePlanService.insert(pSpecialPurchasePlan);
        restResult.setData(pSpecialPurchasePlan);
        return restResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "专项采购计划更新接口",
            notes = "专项采购计划更新接口",
            response = PSpecialPurchasePlan.class
    )
    public RestReturnResult update(@RequestBody PSpecialPurchasePlan pSpecialPurchasePlan) {
        RestReturnResult restResult = RestReturnResult.success();
        pSpecialPurchasePlanService.updateById(pSpecialPurchasePlan);
        restResult.setData(pSpecialPurchasePlan);
        return restResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "专项采购计划通过ID删除接口",
            notes = "专项采购计划通过ID删除接口",
            response = PSpecialPurchasePlan.class
    )
    public RestReturnResult delete(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        pSpecialPurchasePlanService.deleteById(id);
        return restResult;
    }
    
    @PostMapping("/getPSpPlanUnConfirmProjects")
    @ApiOperation(
            value = "专项采购计划-获取存在待确认计划的项目",
            notes = "专项采购计划-获取存在待确认计划的项目",
            response = ExtPSpecialPurchasePlan.class
    )
    public RestReturnResult getPSpPlanUnConfirmProjects() {
        RestReturnResult restResult = RestReturnResult.success();
        List<ExtPSpecialPurchasePlan> extPSpecialPurchasePlans = pSpecialPurchasePlanService.selectPSpPlanUnConfirmProjects();
        restResult.setData(extPSpecialPurchasePlans);
        return restResult;
    }

    @GetMapping("/getCompanyNameByProjectId/{projectId}")
    @ApiOperation(
            value = "专项采购计划-根据项目ID获取项目公司名称",
            notes = "专项采购计划-根据项目ID获取项目公司名称"
    )
    public RestReturnResult<SCompany> getCompanyNameByProjectId(@PathVariable String projectId) {
        RestReturnResult restResult = RestReturnResult.success();
        SProject sProject = sProjectService.selectById(projectId);
        SCompany sCompany = sCompanyService.selectById(sProject.getCompanyId());
        restResult.setData(sCompany);
        return restResult;
    }


}

